#include "DropoutDetectionWidget.h"
#include "ui_dropoutDetectionWidget.h"

#include <QTextCodec>
#include <QProcess>
#include <QDebug>
#include <QMessageBox>
#include <QDateTime>
#include <iostream>
#include <QTimer>

DropoutDetectionWidget::DropoutDetectionWidget(QWidget *parent) : 
    QMainWindow(parent),
    ui(new Ui::DropoutDetectionWidget)
{
    ui->setupUi(this);
    serial = new QSerialPort(this);
    impendanceFlag = nullptr;
    sendNum = 0;
    recvNum = 0;

    // 状态栏
    QStatusBar *sBar = statusBar();
    lblSendNum = new QLabel(this);
    lblPortState = new QLabel(this);
    lblRecvNum = new QLabel(this);
    lblPortState->setText("Connected");
    //设置串口状态标签为绿色 表示已连接状态
    lblPortState->setStyleSheet("color:red");
    lblPortState->setMinimumSize(550, 20);
    // 设置标签最小大小
    lblSendNum->setMinimumSize(100, 20);
    lblRecvNum->setMinimumSize(100, 20);
    setNumOnLabel(lblSendNum, "S: ", sendNum);
    setNumOnLabel(lblRecvNum, "R: ", recvNum);
    sBar->addPermanentWidget(lblPortState);
    sBar->addPermanentWidget(lblSendNum);
    sBar->addPermanentWidget(lblRecvNum);
    getSerialPortList();
    
    // cb_serialPort用于选择串口，btn_openSerialPort用于打开串口
    connect(ui->btn_clear, &QPushButton::clicked, this, &DropoutDetectionWidget::clear_receiveTextEdit);
    connect(ui->btn_send, &QPushButton::clicked, this, &DropoutDetectionWidget::sendTextData);
    connect(ui->btn_scandSerialPort, &QPushButton::clicked, this, &DropoutDetectionWidget::getSerialPortList);
    connect(ui->btn_openSerialPort, &QPushButton::clicked, this, &DropoutDetectionWidget::on_btn_impedanceCheck_clicked);
    // connect(serial, &QSerialPort::readyRead, this, &DropoutDetectionWidget::readData);
    timer_readData = new QTimer(this);
    connect(timer_readData, &QTimer::timeout, this, &DropoutDetectionWidget::readData);

    ui->debugWidget->hide();
    // setAllGreen();
    // setAllRed();
}


DropoutDetectionWidget::~DropoutDetectionWidget() {
    delete ui;
    // std::cout << "~DropoutDetectionWidget()" << std::endl;

    // delete serial;
    // delete lblPortState;
    // delete lblSendNum;
}

// QList<QSerialPortInfo> DropoutDetectionWidget::getSerialPortList(){
void DropoutDetectionWidget::getSerialPortList(){
    if(ui->cb_serialPort->count() != 0){
        ui->cb_serialPort->clear();
    }
    // 自动读取放大器端口
    const auto infos = QSerialPortInfo::availablePorts();
    int index = 0;
    for (const QSerialPortInfo &info : infos){
        QString portDescription = QString("%1 (%2)").arg(info.portName(), info.description());
        ui->cb_serialPort->addItem(portDescription, info.portName());
        if(info.description() == "STM32 Virtual ComPort"){
            ui->cb_serialPort->setCurrentIndex(index);
        }
        // qDebug() << portDescription;
        ++index;
    }
}

void DropoutDetectionWidget::closeSerialPort(){
    timer_readData->stop();
    if (serial->isOpen())
        serial->close();
    ui->btn_impedanceCheck->setText("打开阻抗检测");
    ui->btn_openSerialPort->setText("打开串口");
    ui->cb_serialPort->setEnabled(true);
    //statusBar 状态栏显示端口状态
    lblPortState->setText("CLOSED");
    lblPortState->setStyleSheet("color:red");
}


void DropoutDetectionWidget::readData(){
    if (serial->bytesAvailable() == 0) {return;}
    QByteArray recBuf = serial->readAll();
    QTextCodec *codec = QTextCodec::codecForName("GBK"); // 或使用其他适当的编码
    QString rec_string = codec->toUnicode(recBuf);
    QString str_rev;
    // 接收字节计数
    recvNum += recBuf.size();
    // 状态栏显示计数值
    setNumOnLabel(lblRecvNum, "R: ", recvNum);

    if(ui->chk_rev_hex->checkState() == false){
        if(ui->chk_rev_time->checkState() == Qt::Checked){
            QDateTime nowtime = QDateTime::currentDateTime();
            str_rev = "[" + nowtime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
            str_rev += QString(rec_string).append("\r\n");
        }
        else{
	        // 在当前位置插入文本，不会发生换行。如果没有移动光标到文件结尾，会导致文件超出当前界面显示范围，界面也不会向下滚动。
	        //ui->recvEdit->appendPlainText(buf);
	        if(ui->chk_rev_line->checkState() == Qt::Checked){
	            str_rev = QString(rec_string).append("\r\n");
	        }
	        else
	        {
	            str_rev = QString(rec_string);
	        }
        }
    }
    else{
        // 16进制显示，并转换为大写
        QString str1 = recBuf.toHex().toUpper();//.data();
        // 添加空格
        QString str2;
        for(int i = 0; i<str1.length (); i+=2)
        {
            str2 += str1.mid (i,2);
            str2 += " ";
        }
        if(ui->chk_rev_time->checkState() == Qt::Checked)
        {
            QDateTime nowtime = QDateTime::currentDateTime();
            str_rev = "[" + nowtime.toString("yyyy-MM-dd hh:mm:ss") + "] ";
            str_rev += str2.append("\r\n");
        }
        else
        {
            if(ui->chk_rev_line->checkState() == Qt::Checked)
                str_rev += str2.append("\r\n");
            else
                str_rev = str2;
        }
    }
    ui->receiveEdit->insertPlainText(str_rev);
    ui->receiveEdit->moveCursor(QTextCursor::End);
    decode(&recBuf);
    updateSensorLabel();

}

/**********************************
 * 函数名：decode
 * 功能：解码读取阻抗状态
 * 输入：无
 * 输出：0：成功；-1：失败
 * 备注：无
**********************************/
int DropoutDetectionWidget::decode(QByteArray *data){
    int len = data->length();
    // qDebug() << "len=" << len;
    int firstPkg_index = -1;
    // 反向遍历找倒数第二帧
    for( int i = len-2; i >= 0; --i){
            if( TCPEEG_HEAD_1 == data->at(i) && TCPEEG_HEAD_2 == data->at(i+1)){
                if(firstPkg_index != -1){
                    firstPkg_index = i;
                    break;
                }
                else{
                    firstPkg_index = i;
                    continue;
                }
            }
    }
    // qDebug() << "firstPkg_index=" << firstPkg_index;
    // 没有接收到有效数据
    if( firstPkg_index < 0 ){
        printf(" Frame header verification failed\n");
        return -1;
    }

    // qDebug() << "sizeof(TCPEEG_PACK)=" << sizeof(TCPEEG_PACK);
    
    memcpy(&one_pack, data->constData()+firstPkg_index, sizeof(TCPEEG_PACK));
    
    // qDebug() << dec << one_pack.battery;
    impendanceFlag = reinterpret_cast<ImpendanceFlag*> (&one_pack.impendance);
    // qDebug() << "AF3="<< impendanceFlag->AF3;
    // qDebug() << "CP3="<< impendanceFlag->CP3;
    return 0;
}

void DropoutDetectionWidget::setAllRed(){
    setLabel_red(ui->FP1);
    setLabel_red(ui->FPZ);
    setLabel_red(ui->FP2);
    setLabel_red(ui->AF3);
    setLabel_red(ui->AF4);
    setLabel_red(ui->F7);
    setLabel_red(ui->F5);
    setLabel_red(ui->F3);
    setLabel_red(ui->F1);
    setLabel_red(ui->FZ);
    setLabel_red(ui->F2);
    setLabel_red(ui->F4);
    setLabel_red(ui->F6);
    setLabel_red(ui->F8);
    setLabel_red(ui->FT7);
    setLabel_red(ui->FC5);
    setLabel_red(ui->FC3);
    setLabel_red(ui->FC1);
    setLabel_red(ui->FCZ);
    setLabel_red(ui->FC2);
    setLabel_red(ui->FC4);
    setLabel_red(ui->FC6);
    setLabel_red(ui->FT8);
    setLabel_red(ui->T7);
    setLabel_red(ui->C5);
    setLabel_red(ui->C3);
    setLabel_red(ui->C1);
    setLabel_red(ui->CZ);
    setLabel_red(ui->C2);
    setLabel_red(ui->C4);
    setLabel_red(ui->C6);
    setLabel_red(ui->T8);
    setLabel_red(ui->M1);
    setLabel_red(ui->TP7);
    setLabel_red(ui->CP5);
    setLabel_red(ui->CP3);
    setLabel_red(ui->CP1);
    setLabel_red(ui->CPZ);
    setLabel_red(ui->CP2);
    setLabel_red(ui->CP4);
    setLabel_red(ui->CP6);
    setLabel_red(ui->TP8);
    setLabel_red(ui->M2);
    setLabel_red(ui->P7);
    setLabel_red(ui->P5);
    setLabel_red(ui->P3);
    setLabel_red(ui->P1);
    setLabel_red(ui->PZ);
    setLabel_red(ui->P2);
    setLabel_red(ui->P4);
    setLabel_red(ui->P6);
    setLabel_red(ui->P8);
    setLabel_red(ui->PO7);
    setLabel_red(ui->PO5);
    setLabel_red(ui->PO3);
    setLabel_red(ui->POZ);
    setLabel_red(ui->PO4);
    setLabel_red(ui->PO6);
    setLabel_red(ui->PO8);
    setLabel_red(ui->CB1);
    setLabel_red(ui->O1);
    setLabel_red(ui->OZ);
    setLabel_red(ui->O2);
    setLabel_red(ui->CB2);
    setLabel_red(ui->GND);
}

void DropoutDetectionWidget::setAllGreen(){
    setLabel_green(ui->FP1);
    setLabel_green(ui->FPZ);
    setLabel_green(ui->FP2);
    setLabel_green(ui->AF3);
    setLabel_green(ui->AF4);
    setLabel_green(ui->F7);
    setLabel_green(ui->F5);
    setLabel_green(ui->F3);
    setLabel_green(ui->F1);
    setLabel_green(ui->FZ);
    setLabel_green(ui->F2);
    setLabel_green(ui->F4);
    setLabel_green(ui->F6);
    setLabel_green(ui->F8);
    setLabel_green(ui->FT7);
    setLabel_green(ui->FC5);
    setLabel_green(ui->FC3);
    setLabel_green(ui->FC1);
    setLabel_green(ui->FCZ);
    setLabel_green(ui->FC2);
    setLabel_green(ui->FC4);
    setLabel_green(ui->FC6);
    setLabel_green(ui->FT8);
    setLabel_green(ui->T7);
    setLabel_green(ui->C5);
    setLabel_green(ui->C3);
    setLabel_green(ui->C1);
    setLabel_green(ui->CZ);
    setLabel_green(ui->C2);
    setLabel_green(ui->C4);
    setLabel_green(ui->C6);
    setLabel_green(ui->T8);
    setLabel_green(ui->M1);
    setLabel_green(ui->TP7);
    setLabel_green(ui->CP5);
    setLabel_green(ui->CP3);
    setLabel_green(ui->CP1);
    setLabel_green(ui->CPZ);
    setLabel_green(ui->CP2);
    setLabel_green(ui->CP4);
    setLabel_green(ui->CP6);
    setLabel_green(ui->TP8);
    setLabel_green(ui->M2);
    setLabel_green(ui->P7);
    setLabel_green(ui->P5);
    setLabel_green(ui->P3);
    setLabel_green(ui->P1);
    setLabel_green(ui->PZ);
    setLabel_green(ui->P2);
    setLabel_green(ui->P4);
    setLabel_green(ui->P6);
    setLabel_green(ui->P8);
    setLabel_green(ui->PO7);
    setLabel_green(ui->PO5);
    setLabel_green(ui->PO3);
    setLabel_green(ui->POZ);
    setLabel_green(ui->PO4);
    setLabel_green(ui->PO6);
    setLabel_green(ui->PO8);
    setLabel_green(ui->CB1);
    setLabel_green(ui->O1);
    setLabel_green(ui->OZ);
    setLabel_green(ui->O2);
    setLabel_green(ui->CB2);
    setLabel_green(ui->GND);
}

void DropoutDetectionWidget::updateSensorLabel(){
    impendanceFlag->FP1 ? setLabel_red(ui->FP1) : setLabel_green(ui->FP1);
    impendanceFlag->FPZ ? setLabel_red(ui->FPZ) : setLabel_green(ui->FPZ);
    impendanceFlag->FP2 ? setLabel_red(ui->FP2) : setLabel_green(ui->FP2);
    impendanceFlag->AF3 ? setLabel_red(ui->AF3) : setLabel_green(ui->AF3);
    impendanceFlag->AF4 ? setLabel_red(ui->AF4) : setLabel_green(ui->AF4);
    impendanceFlag->F7 ? setLabel_red(ui->F7) : setLabel_green(ui->F7);
    impendanceFlag->F5 ? setLabel_red(ui->F5) : setLabel_green(ui->F5);
    impendanceFlag->F3 ? setLabel_red(ui->F3) : setLabel_green(ui->F3);
    impendanceFlag->F1 ? setLabel_red(ui->F1) : setLabel_green(ui->F1);
    impendanceFlag->FZ ? setLabel_red(ui->FZ) : setLabel_green(ui->FZ);
    impendanceFlag->F2 ? setLabel_red(ui->F2) : setLabel_green(ui->F2);
    impendanceFlag->F4 ? setLabel_red(ui->F4) : setLabel_green(ui->F4);
    impendanceFlag->F6 ? setLabel_red(ui->F6) : setLabel_green(ui->F6);
    impendanceFlag->F8 ? setLabel_red(ui->F8) : setLabel_green(ui->F8);
    impendanceFlag->FT7 ? setLabel_red(ui->FT7) : setLabel_green(ui->FT7);
    impendanceFlag->FC5 ? setLabel_red(ui->FC5) : setLabel_green(ui->FC5);
    impendanceFlag->FC3 ? setLabel_red(ui->FC3) : setLabel_green(ui->FC3);
    impendanceFlag->FC1 ? setLabel_red(ui->FC1) : setLabel_green(ui->FC1);
    impendanceFlag->FCZ ? setLabel_red(ui->FCZ) : setLabel_green(ui->FCZ);
    impendanceFlag->FC2 ? setLabel_red(ui->FC2) : setLabel_green(ui->FC2);
    impendanceFlag->FC4 ? setLabel_red(ui->FC4) : setLabel_green(ui->FC4);
    impendanceFlag->FC6 ? setLabel_red(ui->FC6) : setLabel_green(ui->FC6);
    impendanceFlag->FT8 ? setLabel_red(ui->FT8) : setLabel_green(ui->FT8);
    impendanceFlag->T7 ? setLabel_red(ui->T7) : setLabel_green(ui->T7);
    impendanceFlag->C5 ? setLabel_red(ui->C5) : setLabel_green(ui->C5);
    impendanceFlag->C3 ? setLabel_red(ui->C3) : setLabel_green(ui->C3);
    impendanceFlag->C1 ? setLabel_red(ui->C1) : setLabel_green(ui->C1);
    impendanceFlag->CZ ? setLabel_red(ui->CZ) : setLabel_green(ui->CZ);
    impendanceFlag->C2 ? setLabel_red(ui->C2) : setLabel_green(ui->C2);
    impendanceFlag->C4 ? setLabel_red(ui->C4) : setLabel_green(ui->C4);
    impendanceFlag->C6 ? setLabel_red(ui->C6) : setLabel_green(ui->C6);
    impendanceFlag->T8 ? setLabel_red(ui->T8) : setLabel_green(ui->T8);
    impendanceFlag->M1 ? setLabel_red(ui->M1) : setLabel_green(ui->M1);
    impendanceFlag->TP7 ? setLabel_red(ui->TP7) : setLabel_green(ui->TP7);
    impendanceFlag->CP5 ? setLabel_red(ui->CP5) : setLabel_green(ui->CP5);
    impendanceFlag->CP3 ? setLabel_red(ui->CP3) : setLabel_green(ui->CP3);
    impendanceFlag->CP1 ? setLabel_red(ui->CP1) : setLabel_green(ui->CP1);
    impendanceFlag->CPZ ? setLabel_red(ui->CPZ) : setLabel_green(ui->CPZ);
    impendanceFlag->CP2 ? setLabel_red(ui->CP2) : setLabel_green(ui->CP2);
    impendanceFlag->CP4 ? setLabel_red(ui->CP4) : setLabel_green(ui->CP4);
    impendanceFlag->CP6 ? setLabel_red(ui->CP6) : setLabel_green(ui->CP6);
    impendanceFlag->TP8 ? setLabel_red(ui->TP8) : setLabel_green(ui->TP8);
    impendanceFlag->M2 ? setLabel_red(ui->M2) : setLabel_green(ui->M2);
    impendanceFlag->P7 ? setLabel_red(ui->P7) : setLabel_green(ui->P7);
    impendanceFlag->P5 ? setLabel_red(ui->P5) : setLabel_green(ui->P5);
    impendanceFlag->P3 ? setLabel_red(ui->P3) : setLabel_green(ui->P3);
    impendanceFlag->P1 ? setLabel_red(ui->P1) : setLabel_green(ui->P1);
    impendanceFlag->PZ ? setLabel_red(ui->PZ) : setLabel_green(ui->PZ);
    impendanceFlag->P2 ? setLabel_red(ui->P2) : setLabel_green(ui->P2);
    impendanceFlag->P4 ? setLabel_red(ui->P4) : setLabel_green(ui->P4);
    impendanceFlag->P6 ? setLabel_red(ui->P6) : setLabel_green(ui->P6);
    impendanceFlag->P8 ? setLabel_red(ui->P8) : setLabel_green(ui->P8);
    impendanceFlag->PO7 ? setLabel_red(ui->PO7) : setLabel_green(ui->PO7);
    impendanceFlag->PO5 ? setLabel_red(ui->PO5) : setLabel_green(ui->PO5);
    impendanceFlag->PO3 ? setLabel_red(ui->PO3) : setLabel_green(ui->PO3);
    impendanceFlag->POZ ? setLabel_red(ui->POZ) : setLabel_green(ui->POZ);
    impendanceFlag->PO4 ? setLabel_red(ui->PO4) : setLabel_green(ui->PO4);
    impendanceFlag->PO6 ? setLabel_red(ui->PO6) : setLabel_green(ui->PO6);
    impendanceFlag->PO8 ? setLabel_red(ui->PO8) : setLabel_green(ui->PO8);
    impendanceFlag->CB1 ? setLabel_red(ui->CB1) : setLabel_green(ui->CB1);
    impendanceFlag->O1 ? setLabel_red(ui->O1) : setLabel_green(ui->O1);
    impendanceFlag->OZ ? setLabel_red(ui->OZ) : setLabel_green(ui->OZ);
    impendanceFlag->O2 ? setLabel_red(ui->O2) : setLabel_green(ui->O2);
    impendanceFlag->CB2 ? setLabel_red(ui->CB2) : setLabel_green(ui->CB2);
}

void DropoutDetectionWidget::setLabel_green(QLabel *label){
    // QImage* img=new QImage;
    // //加载图像
    // if(! ( img->load(":/pic/pic/circle_green.png") ) ) {
    //     return ;
    //     delete img;
    // }
    // QImage* imgScaled = new QImage;
    // *imgScaled=img->scaled(40,
    //                        40,
    //                        Qt::KeepAspectRatio);
    // label->setPixmap(QPixmap::fromImage(*imgScaled));
    label->setStyleSheet("border-image: url(:/pic/pic/circle_green.png);");
}

void DropoutDetectionWidget::setLabel_red(QLabel *label){
    // QImage* img=new QImage;
    // //加载图像
    // if(! ( img->load(":/pic/pic/circle_red.png") ) ) {
    //     return ;
    //     delete img;
    // }
    // QImage* imgScaled = new QImage;
    // *imgScaled=img->scaled(40,
    //                        40,
    //                        Qt::KeepAspectRatio);
    // label->setPixmap(QPixmap::fromImage(*imgScaled));
    label->setStyleSheet("border-image: url(:/pic/pic/circle_red.png);");
}

void DropoutDetectionWidget::sendTextData(){
    QByteArray array;
    //Hex复选框
    // if(ui->chk_send_hex->checkState() == Qt::Checked){
    if(1){
        array = QByteArray::fromHex(ui->sendEdit->toPlainText().toUtf8());
    }else{
        array = ui->sendEdit->toPlainText().toLocal8Bit();
    }
    // if(ui->chk_send_line->checkState() == Qt::Checked){
    if(1){
        array.append("\r\n");
    }
    // 如发送成功，会返回发送的字节长度。失败，返回-1。
    int a = serial->write(array);
    // 发送字节计数并显示
    if(a > 0){
        // 发送字节计数
        sendNum += a;
        // 状态栏显示计数值
        setNumOnLabel(lblSendNum, "S: ", sendNum);
    }
}

// 状态栏标签显示计数值
void DropoutDetectionWidget::setNumOnLabel(QLabel *lbl, QString strS, long num){
    // 标签显示
    QString strN;
    strN.sprintf("%ld", num);
    QString str = strS + strN;
    lbl->setText(str);
}

void DropoutDetectionWidget::clear_receiveTextEdit(){
    recvNum = 0;
    setNumOnLabel(lblRecvNum, "S: ", 0);
    ui->receiveEdit->clear();
}

// void DropoutDetectionWidget::handleSerialError(QSerialPort::SerialPortError error){
//     if (error == QSerialPort::ResourceError) {
//         qDebug() << "串口设备错误:" << serial->errorString();
//         serial->close();
//     } else {
//         qDebug() << "串口错误:" << serial->errorString();
//     }
// }

void DropoutDetectionWidget::on_btn_impedanceCheck_clicked(bool checked){
    static bool impedanceCheck_enable = false;
    if(!impedanceCheck_enable){
        impedanceCheck_enable = true;
        ui->btn_impedanceCheck->setText("关闭阻抗检测");
        ui->btn_openSerialPort->setText("关闭串口");

        QString portName = ui->cb_serialPort->itemData(ui->cb_serialPort->currentIndex()).toString();
        serial->setPortName(portName);
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);

        if (!serial->open(QIODevice::ReadWrite)) {
            qDebug() << "Failed to open port:" << serial->errorString();
        }
        else {
            // qDebug() << "Port opened successfully.";
            timer_readData->start(500);
            ui->cb_serialPort->setEnabled(false);
            //statusBar 状态栏显示端口状态
            QString sm = "%1 OPENED, %2, 8, NONE, OneStop";
            QString status = sm.arg(serial->portName()).arg(serial->baudRate());
            lblPortState->setText(status);
            lblPortState->setStyleSheet("color:green");
        }
    }
    else{
        impedanceCheck_enable = false;
        closeSerialPort();
    }
}

void DropoutDetectionWidget::on_btn_debug_clicked(bool checked){
    static bool debug_enable = false;
    if(!debug_enable){
        debug_enable = true;
        ui->btn_debug->setText("关闭调试");
        ui->sensorWidget->hide();
        ui->debugWidget->show();
    }
    else{
        debug_enable = false;
        ui->btn_debug->setText("调试");
        ui->sensorWidget->show();
        ui->debugWidget->hide();
    }
}

void DropoutDetectionWidget::on_btn_getDeviceInfo_clicked(bool checked){
    ui->sendEdit->setText(DEVICE_INFO);
    sendTextData();
}


void DropoutDetectionWidget::on_btn_dropout_clicked(bool checked){
    static bool en_flag = false;
    if(!en_flag){
        en_flag = true;
        ui->btn_dropout->setText("关闭检测");
        ui->sendEdit->setText(OPEN_DROPOUTDETECTION);
        sendTextData();
    }
    else{
        en_flag = false;
        ui->btn_dropout->setText("开启检测");
        ui->sendEdit->setText(CLOSE_DROPOUTDETECTION);
        sendTextData();
    }
}
void DropoutDetectionWidget::on_btn_USBTransmission_clicked(bool checked){
    static bool en_flag = false;
    if(!en_flag){
        en_flag = true;
        ui->btn_USBTransmission->setText("关闭传输");
        ui->sendEdit->setText(OPEN_USBTRANSMISSION);
        sendTextData();
    }
    else{
        en_flag = false;
        ui->btn_USBTransmission->setText("开启传输");
        ui->sendEdit->setText(CLOSE_USBTRANSMISSION);
        sendTextData();
    }
}
void DropoutDetectionWidget::on_btn_changeCurrent_clicked(bool checked){
    qDebug() << "暂时不支持该功能";
}

void DropoutDetectionWidget::closeEvent(QCloseEvent *event)
{
    closeSerialPort();
    emit widgetClose(WINID::DropoutDetectionWidget);
    std::cout << "DropoutDetection Windows is closed!" << std::endl;
}

void DropoutDetectionWidget::showImpendanceFlag(){
    if(impendanceFlag == nullptr){
        qDebug() << "ImpendanceFlag NULL!";
        return;
    }
    int a = 3;
    QString showString = QString("FP1:%1,FPZ:%2").arg(1).arg(2);
    qDebug() << "showString=" << showString;

    std::cout << "FP1:" << a << ",FPZ:" << static_cast<int>(impendanceFlag->FPZ)
        << ",FP2:" << impendanceFlag->FP2<< std::endl;
        ;
}


 